package com.example.controller;

import com.example.entity.Goods;
import com.example.entity.User;
import com.example.service.GoodsService;
import com.example.service.TeacherService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private GoodsService goodsService;
    //点击用户头像查询用户作品
    @Autowired
    private UserService userService;
    @RequestMapping("/homepage")
    public String list(Model model,int uid){
        User teacher=teacherService.getTeacherByUid(uid);
        System.out.println(teacher.getUid());
        List<Goods> goods=goodsService.getGoodsByAuthor(teacher.getName());
        model.addAttribute("teacher",teacher);
        model.addAttribute("goods",goods);
        return "teacher/homepage";
    }

    //条件查询，查询teacher的课程
    //搜索
    @RequestMapping("/list")
    public ModelAndView showGoodsByTeacher(@RequestParam("name") String name,@RequestParam(required = false)String type) {
        ModelAndView mv = new ModelAndView();
        List<Goods> goodsList ;
        //条件查询
        if (StringUtils.hasText(type)) {  // 使用Spring工具类判断非空
            // 同时传递类型和老师姓名
             goodsList = goodsService.searchGoodsByType(type,name);
        }else {
            //空就展示所有商品
            goodsList = teacherService.getGoodsByTeacherName(name);
        }
        // 调用 Service 层的方法查询商品信息

        // 将查询结果添加到 Model 中，供 JSP 页面使用
        mv.addObject("goodsList", goodsList);
        mv.addObject("searchType",type);
        // 保留老师姓名用于页面回显
        mv.addObject("teacherName", name);
        // 返回 JSP 页面视图名称
        mv.setViewName("teacher/list");
//        return "/teacher/home";
        return mv;
    }
    @GetMapping("/add")
    public String toAddPage() {
        return "teacher/add";
    }

    @PostMapping("/add")
    public String add(Goods goods) throws UnsupportedEncodingException {
        teacherService.addGoods(goods);
        // 对中文参数进行URL编码
        return "redirect:/teacher/list?name=" + URLEncoder.encode(goods.getAuthor(), "UTF-8");
    }

    @GetMapping("/edit/{gid}")
    public String toEditPage(@PathVariable("gid") Integer gid, Model model) {
        Goods goods = goodsService.getGoodsById(gid);
        model.addAttribute("goods", goods);
        return "teacher/edit";
    }

    @PostMapping("/edit")
    public String edit(Goods goods) throws UnsupportedEncodingException {
        teacherService.updateGoods(goods);
        // 对中文参数进行URL编码
        return "redirect:/teacher/list?name=" + URLEncoder.encode(goods.getAuthor(), "UTF-8");
    }

    @GetMapping("/delete/{gid}")
    public String delete(@PathVariable("gid") Integer gid) throws UnsupportedEncodingException {
        Goods goods = goodsService.getGoodsById(gid);
        teacherService.deleteGoods(gid);
        // 对中文参数进行URL编码
        return "redirect:/teacher/list?name=" + URLEncoder.encode(goods.getAuthor(), "UTF-8");
    }


    //直播跳转
    @GetMapping("/video")
    public ModelAndView showVideo() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("video");
        return mv;
    }

   //修改
   @GetMapping("/update/{uid}")
   public String toFindId(@PathVariable("uid") Integer uid, Model model) {
       User user = userService.getUserById(uid);
       model.addAttribute("user", user);
       return "teacher/update";
   }

    @PostMapping("/update")
    public String update(User user) throws UnsupportedEncodingException {

        userService.updateUser(user);
        // 对中文参数进行URL编码
        return "redirect:/teacher/list?name=" + URLEncoder.encode(user.getName(), "UTF-8");
    }

    @GetMapping("/yield")
    public ModelAndView showYield() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("yield");
        return mv;
    }
}
